package com.dede.screensaverhelper.tile

import android.os.Build
import android.service.quicksettings.Tile
import android.service.quicksettings.TileService
import android.support.annotation.RequiresApi
import android.util.Log
import com.dede.screensaverhelper.activity.DreamTempActivity
import com.dede.screensaverhelper.activity.MainActivity
import com.dede.screensaverhelper.util.Utils
import org.jetbrains.anko.startActivity

/**
 * @author hsh
 * @date 2018/2/2 16:57.
 * @doc
 */
@RequiresApi(Build.VERSION_CODES.N)
class DreamNowTile : TileService() {

    private val TAG = "DreamNowTile"

    override fun onStartListening() {
        super.onStartListening()
        val screenSaverState = Utils.getScreenSaverState(this)
        qsTile.state = if (screenSaverState) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE
        qsTile.updateTile()
    }

    override fun onStopListening() {
        super.onStopListening()
        Log.i(TAG, "onStopListening: ")
    }

    override fun onClick() {
        super.onClick()
        Log.i(TAG, "onClick: ")
        dream()
    }

    private fun dream() {
        val screenSaverState = Utils.getScreenSaverState(this)
        if (!screenSaverState) {
            startActivity<MainActivity>()
        } else {
            startActivity<DreamTempActivity>()
        }
    }
}